package com.taobao.machinesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.machinesystem.entity.McAddressbook;
import com.taobao.machinesystem.entity.McInspect;
import com.taobao.machinesystem.entity.SysCompan;
import com.taobao.machinesystem.entity.SysUser;
import com.taobao.machinesystem.entity.vo.McInspectQueryVo;
import com.taobao.machinesystem.mapper.McInspectMapper;
import com.taobao.machinesystem.service.McAddressbookService;
import com.taobao.machinesystem.service.McInspectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taobao.machinesystem.service.McPlan1Service;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 单次巡查表 服务实现类
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Service
public class McInspectServiceImpl extends ServiceImpl<McInspectMapper, McInspect> implements McInspectService {

    @Autowired
    private McAddressbookService mcAddressbookService;

    @Autowired
    private McPlan1Service mcPlan1Service;

    @Override
    public Page<McInspect> listMcInspect(Integer currentPage, Integer size, McInspectQueryVo mcInspectQueryVo) {
        Page<McInspect> mcInspect = new Page<>(currentPage,size);

        QueryWrapper<McInspect> queryWrapper = new QueryWrapper<>();

        if (!"".equals(mcInspectQueryVo.getInspectCompan()) && mcInspectQueryVo.getInspectCompan()!=null){
            queryWrapper.like("inspect_compan",mcInspectQueryVo.getInspectCompan());
        }

        return baseMapper.selectPage(mcInspect,queryWrapper);
    }

    /**
     * 创建单次巡查
     * @param mcInspect
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createMcInspect(McInspect mcInspect) {

        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        mcInspect.setCreatedBy(sysUser.getUsername());

        int i = baseMapper.insert(mcInspect);

        String inspectPerson = mcInspect.getInspectPerson();
        boolean flag = mcAddressbookService.selectBookInspectByName(inspectPerson);
        McAddressbook mcAddressbook = new McAddressbook();





        if (!StringUtils.isEmpty(mcInspect.getInspectPialid())){
            mcAddressbook.setInspectId(mcInspect.getInspectPialid());
            //todo 计划巡查标志
            mcPlan1Service.updateState(mcInspect.getInspectPialid());
        }

        if (!flag){
            mcAddressbook.setName(mcInspect.getInspectPerson());
            mcAddressbook.setCompan(mcInspect.getInspectCompan());
            mcAddressbook.setCompanId(mcInspect.getInspectCompanid());
            mcAddressbook.setIdCard(mcInspect.getInspectIdcard());
            mcAddressbook.setPhone(mcInspect.getInspectPhone());
            mcAddressbook.setPhontype(mcInspect.getInspectPhontype());

            mcAddressbookService.save(mcAddressbook);
        }


        return i > 0 ? true : false;
    }
}
